﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_Social_FbPageSelector : System.Web.UI.UserControl
{

	public BO.Account Account { get; set; }
	string[] delimiter = new string[]{"@"};


    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
			LoadData();
    }

	private void LoadData()
	{
		if (string.IsNullOrEmpty(Account.FbToken))
		{
			this.Visible = false;
			return;
		}

		var pages = Logic.BusinessLogic.SocialNetworks.Data.GetUserFbPages(Account.FbToken);
		foreach (var page in pages)
		{
			var li = new ListItem(page.name, page.id + delimiter[0] + page.access_token);
			rblPages.Items.Add(li);
			if (Account.FbPageID.HasValue && Account.FbPageID.Value == page.id)
				li.Selected = true;
		}
		
	}

	protected void btnSave_Click(object sender, EventArgs e)
	{
		SetSelectedPageValues();
	}

	public void SetSelectedPageValues()
	{
		if(rblPages.SelectedIndex!= -1)
		{
			Account.FbPageName = rblPages.SelectedItem.Text;
			var values = rblPages.SelectedValue.Split(delimiter, StringSplitOptions.None);
			Account.FbPageID = Convert.ToInt64(values[0]);
			Account.FbPageToken = values[1];
		}

		Account.Update();
	}

}